package com.uft.remote;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;


public class UftRemote extends JSch{
	private String hostname;
	private String username;
	private String password;
	private int port=22;
	private String knownHostsFilename;
	 private final static String CURRENT_DIRECTORY = ".";
	 private final static String PARENT_DIRECTORY = "..";
	public UftRemote(String hName, String uName, String pwd, int port){
		super();	
		this.hostname=hName;
		this.username=uName;
		this.password=pwd;
		this.port=port;
		knownHostsFilename=System.getProperty("user.home")+"/.ssh/known_hosts";
		initialize();
	}
	
	public UftRemote(String hName, String uName, String pwd, int port, String knownHostsFilename){
		super();	
		this.hostname=hName;
		this.username=uName;
		this.password=pwd;
		this.port=port;
		if(knownHostsFilename != null && knownHostsFilename.equalsIgnoreCase("")){
			this.knownHostsFilename=knownHostsFilename;
		}else{
			this.knownHostsFilename=System.getProperty("user.home")+"/.ssh/known_hosts";
		}
		initialize();
		
	}
	public void initialize(){
		if(knownHostsFilename != null && ! knownHostsFilename.equalsIgnoreCase("")){
			System.out.println("knownHostFile: "+knownHostsFilename);
			try {
				setKnownHosts(knownHostsFilename);
			} catch (JSchException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public boolean testSSHConnection(){
		 boolean result=false;
		 try{
				Session session=getSession(username,hostname,port);
				session.setPassword(password);
				session.setConfig("StrictHostKeyChecking", "no");
				session.connect();
				if(session.isConnected()){
					result=true;
				}
				session.disconnect();
			}catch(JSchException e){
				e.printStackTrace();
				
			} 
		 return result;
	 }
	
	public String getFile(String from, String to){
		String resultFile= "";
		try{
			
			Session session=getSession(username,hostname,port);
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect();
			Channel channel = session.openChannel("sftp");
			channel.connect();
			ChannelSftp sftpChannel = (ChannelSftp) channel;
            sftpChannel.get(from, to);
            sftpChannel.exit();
            session.disconnect();
		}catch(JSchException e){
			e.printStackTrace();
			
		} catch (SftpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return resultFile;
	}
	public List<String> getFileList(String filPath){
		List<String> fileList= new ArrayList<String>();
		try{
			Session session=getSession(username,hostname,port);
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect();
			Channel channel = session.openChannel("sftp");
			channel.connect();
			ChannelSftp sftpChannel = (ChannelSftp) channel;
			//sftpChannel.cd(filPath);
			Vector<LsEntry> v  = sftpChannel.ls(filPath);
			for (LsEntry i : v){
				if (!i.getAttrs().isDir()){
					String file = new String(i.getFilename());
					if (!file.equals(CURRENT_DIRECTORY) || !file.equals(PARENT_DIRECTORY)){
						
						fileList.add(i.getFilename());
					}
				}
					
			}
				

		}catch(JSchException e){
			e.printStackTrace();
			
		} catch (SftpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return fileList;
	}
	
	
}
